home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Simulating a Memory Manager
- Date: 12 Apr 1996 11:27:52 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4km7b8INNt4o@keats.ugrad.cs.ubc.ca>
- References: <4kldef$png@news.service.uci.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4kldef$png@news.service.uci.edu>,
- Alex Wu <awu@hydra.acs.uci.edu> wrote:
- >Hi, maybe you guys can help me, Im having problems moving around in memory.
- >
- > Okay Im suppose to write a mock-memory manager, that keeps tracks of
- >a list of pointers that points to memory blocks are in use, and a list
- >of open 'holes' in the memory. At the beginning of each block, there
- >is a field for tag (nominate if it's full or open) and size (# of bytes
- >in use).
-
- >char *blocks[100]; /* keeps tracks of pointers that points to the
- > used blocks */
-
- This is lame, because it restricts you to allocating only a total of 100 blocks
- of any. It would be better to keep the blocks in a linked list _within_
- the actual allocation zone.
-
- >struct template
- >{
- > short tag;
- > short size;
- >};
- >
- >char MM[32768]; /* size of the memory block we are keeping */
- >char *MEM_PTR = MM; /* points to the beginning of the block */
- >
- >
- > Okay, we first initialize the memory, so it's just one big hole.
- >
- >struct template *HEAD
- >
- >init_mem()
- >{
- > HEAD->tag = TRUE; /* it's a hole */
- > HEAD->size = 32768;
- >}
-
- You are missing a semicolon after HEAD. Secondly, HEAD doesn't point anywhere,
- so you can't assign to HEAD->tag or HEAD->size.
-
- > Now it has to handle requests. n is the number of bytes that I have to
- >set aside. Once I set it aside, I have to return the begining of the block.
- >And of course adjust any open holes.
-
- Have a look at the example malloc() implementation in the K&R2, the chapter on
- the UNIX system interface, keeping in mind that ANSI C prohibits the external
- redefinition of standard library functions like malloc() in a hosted
- environment.
- --
-
-